package com.lin.mr.maizilaundry.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lin.mr.maizilaundry.R;
import com.lin.mr.maizilaundry.entity.Means;
import com.lin.mr.maizilaundry.myadapter.MeansAdapter;
import com.lin.mr.maizilaundry.utils.VolleyUtils;

import java.util.List;

/**
 * 服务范围
 */
public class ServeActivity extends Activity {
    private PullToRefreshListView pullToRefreshListView;
    private MeansAdapter meansAdapter;
    private ListView listView;
    private List<Means.MeansInfoBean> list;
    private String URL = "http://cloud.bmob.cn/d9f6840be6bb07cf/service_test?clive=means";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_serve);
        pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
        VolleyUtils.requestByGet(URL, "ServeActivity", new VolleyUtils.CallBack() {
            @Override
            public void returnData(Object result) {
                if (result != null) {
                    Gson gson = new Gson();
                    Means means = gson.fromJson((String) result, Means.class);
                    list = means.getMeansInfo();
                    meansAdapter = new MeansAdapter(ServeActivity.this, list);
                    pullToRefreshListView.setAdapter(meansAdapter);
                } else {
                    Toast.makeText(ServeActivity.this, "请检查网络！", Toast.LENGTH_SHORT).show();
                }
            }
        });
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);//两端刷新
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            //下拉
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                Toast.makeText(ServeActivity.this, "下拉刷新", Toast.LENGTH_SHORT).show();
                if (list != null)
                    list.clear();
                //注意下面的代码不能写在else中，否则清空之后不会刷新
                VolleyUtils.requestByGet(URL, "ServeActivity", new VolleyUtils.CallBack() {
                    @Override
                    public void returnData(Object result) {
                        if (result != null) {
                            Gson gson = new Gson();
                            Means means = gson.fromJson((String) result, Means.class);
                            list = means.getMeansInfo();
                            meansAdapter = new MeansAdapter(ServeActivity.this, list);
                            pullToRefreshListView.setAdapter(meansAdapter);
                            pullToRefreshListView.onRefreshComplete();
                        } else {
                            Toast.makeText(ServeActivity.this, "请检查网络！", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pullToRefreshListView.onRefreshComplete();
                    }
                }, 2000);

            }

            //上拉
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                Toast.makeText(ServeActivity.this, "上拉刷新", Toast.LENGTH_SHORT).show();
                VolleyUtils.requestByGet(URL, "ServeActivity", new VolleyUtils.CallBack() {
                    @Override
                    public void returnData(Object result) {
                        if (result != null) {
                            Gson gson = new Gson();
                            Means means = gson.fromJson((String) result, Means.class);
                            for (int i = 0; i < means.getMeansInfo().size(); i++) {
                                Means.MeansInfoBean meansInfo = means.getMeansInfo().get(i);
                                list.add(meansInfo);
                            }
                        } else {
                            Toast.makeText(ServeActivity.this, "请检查网络！", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pullToRefreshListView.onRefreshComplete();
                    }
                }, 2000);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        VolleyUtils.cancelRequest("ServeActivity");
    }
}
